using Cmpp.Client.Cmpp3Message.Utility;

namespace Cmpp.Client.Cmpp3Message {
    public class Cmpp3DeliverRequest : Cmpp3Response {
        private readonly string m_destinationId;
        private readonly string m_linkId;
        private readonly string m_messageContent;
        private readonly CmppMessageFormat m_messageFormat;
        private readonly ulong m_messageId;
        private readonly byte m_messageLength;
        private readonly byte m_registeredDelivery;
        private readonly string m_serviceId;
        private readonly string m_sourceTerminalId;
        private readonly byte m_sourceTerminalType;
        private readonly byte m_tpPid;
        private readonly byte m_tpUdhi;

        public Cmpp3DeliverRequest(CmppMessageReader reader) : base(reader) {
            m_messageId = reader.ReadUInt64();
            m_destinationId = reader.ReadString(21);
            m_serviceId = reader.ReadString(10);
            m_tpPid = reader.ReadByte();
            m_tpUdhi = reader.ReadByte();
            m_messageFormat = (CmppMessageFormat)reader.ReadByte();
            m_sourceTerminalId = reader.ReadString(32);
            m_sourceTerminalType = reader.ReadByte();
            m_registeredDelivery = reader.ReadByte();
            m_messageLength = reader.ReadByte();
            byte[] byteContent = reader.ReadBytes(m_messageLength);
            m_messageContent = GetContentString(m_messageFormat, byteContent);
            m_linkId = reader.ReadString(20);
        }

        public ulong MessageId {
            get { return m_messageId; }
        }

        public string DestinationId {
            get { return m_destinationId; }
        }

        public string ServiceId {
            get { return m_serviceId; }
        }

        public byte TpPid {
            get { return m_tpPid; }
        }

        public byte TpUdhi {
            get { return m_tpUdhi; }
        }

        public CmppMessageFormat MessageFormat {
            get { return m_messageFormat; }
        }

        public string SourceTerminalId {
            get { return m_sourceTerminalId; }
        }

        public byte SourceTerminalType {
            get { return m_sourceTerminalType; }
        }

        public byte RegisteredDelivery {
            get { return m_registeredDelivery; }
        }

        public byte MessageLength {
            get { return m_messageLength; }
        }

        public string MessageContent {
            get { return m_messageContent; }
        }

        public string LinkId {
            get { return m_linkId; }
        }


        private static string GetContentString(CmppMessageFormat format, byte[] bytes) {
            switch (format) {
                case CmppMessageFormat.Binary:
                case CmppMessageFormat.ASCII:
                    return DataConvert.BytesToString(bytes);
                case CmppMessageFormat.UCS2:
                    return DataConvert.UCS2BytesToString(bytes);
                case CmppMessageFormat.GBK:
                    return DataConvert.GBKBytesToString(bytes);
                default:
                    return string.Empty;
            }
        }

    }
}